<template>
  <a-select :options="options" v-model:value="model" @change="onChange" placeholder="请选择"></a-select>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { getByCode } from '@/api/dict'
import { useGlobalStore } from '@/store/global'
import { findItemInArr } from '@/mixin'

const global = useGlobalStore()

const props = defineProps(['value', 'code'])
const emit = defineEmits(['update:value'])
const options = ref([])
const rows = ref([])

const model = ref(props.value)

onMounted(() => {
  load()
})
watch(() => props.value, (v) => {
  model.value = v
})

const load = async () => {
  const all = await global.getDicts()
  const item = findItemInArr(all,i=>i.code == props.code)
  const arr = item.values || [];
  rows.value = arr;
  options.value = arr.map((item) => {
    return {
      value: item.value,
      label: item.value
    }
  })
}

const onChange = e => {
  emit('update:value', e)
}
</script>

<style lang="less" scoped></style>